package com.ssh.webssh.service;

import com.ssh.webssh.pojo.Result;
import feign.Logger;
import feign.Param;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.codec.Decoder;
import feign.codec.Encoder;
import feign.form.spring.SpringFormEncoder;
import feign.form.spring.converter.SpringManyMultipartFilesReader;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.cloud.openfeign.support.SpringEncoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@FeignClient(name = "dayu-api",url = "http://127.0.0.1",configuration = FeignService.FeiginConfig.class)
@EnableFeignClients
public interface FeignService {
    @PostMapping(value = "${api.upload.uri}",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    Result uploadDataFile(@RequestParam Map<String, String> map, @Param(value = "file") MultipartFile file);

    @RequestMapping(value = "/{fileURI}")
    byte[] download(@PathVariable("fileURI") String fileURI);

    @Configuration
    class FeiginConfig implements RequestInterceptor {

        @Override
        public void apply(RequestTemplate requestTemplate) {

        }

        @Autowired
        private ObjectFactory<HttpMessageConverters> messageConverters;
        @Bean
        public Encoder feignFormEncoder () {
            return new SpringFormEncoder(new SpringEncoder(messageConverters));
        }

        @Bean
        public Decoder feignDecoder () {
            List<HttpMessageConverter<?>> springConverters =
                    messageConverters.getObject().getConverters();

            List<HttpMessageConverter<?>> decoderConverters =
                    new ArrayList<>(springConverters.size() + 1);

            decoderConverters.addAll(springConverters);
            decoderConverters.add(new SpringManyMultipartFilesReader(4096));

            HttpMessageConverters httpMessageConverters = new HttpMessageConverters(decoderConverters);

            return new SpringDecoder(() -> httpMessageConverters);
        }

        @Bean
        public Logger.Level feignLoggerLevel(){
            return Logger.Level.FULL;
        }

        @Bean
        public Logger feignLogger(){
            return new Logger(){
                @Override
                protected void log(String s, String s1, Object... objects) {
//                    log.info(String.format(methodTag(s) + s1, objects));
                }
            };
        }
    }
}
